package com.stephen.bp.model;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

import lombok.Data;

/**
 * @author qiuwenwei
 */
@Data
public class Room {
    final String roomId;
    boolean start = false;
    boolean finish = false;
    Long timeout;
    int stage;
    String stageName = "第一阶段";
    ArrayList<Action[]> stageAction = new ArrayList<>();
    User[] users;
    User aUser;
    User bUser;
    ArrayList<Team> canSelectTeam;
    ArrayList<Team> aSelectTeam = new ArrayList<>();
    Set<String> aTimeoutSelectTeam = new HashSet<>();
    ArrayList<Team> bSelectTeam = new ArrayList<>();
    Set<String> bTimeoutSelectTeam = new HashSet<>();

    ArrayList<Team> aBanTeam = new ArrayList<>();
    Set<String> aTimeoutBanTeam = new HashSet<>();

    ArrayList<Team> bBanTeam = new ArrayList<>();
    Set<String> bTimeoutBanTeam = new HashSet<>();


    ArrayList<Team> aCanSelectTeam = new ArrayList<>();
    ArrayList<Team> bCanSelectTeam = new ArrayList<>();

    ArrayList<Team> aFinalSelectTeam = new ArrayList<>();
    Set<String> aTimeoutFinalSelectTeam = new HashSet<>();


    ArrayList<Team> bFinalSelectTeam = new ArrayList<>();
    Set<String> bTimeoutFinalSelectTeam = new HashSet<>();

    public void setStage(int stage) {
        this.stage = Math.min(stage, stageAction.size() - 1);
    }

    public ArrayList<Team> getSelectTeam(String role) {
        if ("第一阶段".equals(stageName)) {
            return canSelectTeam;
        } else {
            return "A".equals(role) ? aCanSelectTeam : bCanSelectTeam;
        }
    }

    //BP 第一阶段:双方战队通过队长群抽签决定 BP 的先后顺序
    // 抽签胜利方决定本方战队 BP 先后顺序。A/B 两队按照以下顺序进行:
    //1 A 战队选择1支球队-
    //2 B 战队选择1支球队-
    //3 B 战队禁用1支球队-
    //4 A 战队禁用1支球队-
    //5 B 战队选择1支球队-
    //6 A 战队选择2支球队-
    //7 B 战队选择1支球队
    // 双方战队不能选择或禁用已被双方选择或禁用的球队BP
    //
    // 第二阶段:双方战队进行球队对阵分配，双方只可从本方战队第一阶段选择的三支球队中选择。每支球队在本轮只能使用一次。
    //第一局 8 A 战队先选择球队 9 B 战队后选球队;
    //第二局 10 B战队先选球队， 11  A 战队后选球队;
    //第三局 12 A 战队先选球队， 13 B 战队后选球队。
    public Room(String roomId, ArrayList<Team> canSelectTeam) {
        this.roomId = roomId;
        this.canSelectTeam = canSelectTeam;
        stage = 0;
        stageAction.add(new Action[]{Action.SELECT, Action.WAIT});
        stageAction.add(new Action[]{Action.WAIT, Action.SELECT});
        stageAction.add(new Action[]{Action.WAIT, Action.BAN});
        stageAction.add(new Action[]{Action.BAN, Action.WAIT});
        stageAction.add(new Action[]{Action.WAIT, Action.SELECT});
        stageAction.add(new Action[]{Action.SELECT, Action.WAIT});
        stageAction.add(new Action[]{Action.SELECT, Action.WAIT});
        stageAction.add(new Action[]{Action.WAIT, Action.SELECT});
        stageAction.add(new Action[]{Action.FINISH_ONE, Action.FINISH_ONE});
        stageAction.add(new Action[]{Action.SELECT, Action.WAIT});
        stageAction.add(new Action[]{Action.WAIT, Action.SELECT});
        stageAction.add(new Action[]{Action.WAIT, Action.SELECT});
        stageAction.add(new Action[]{Action.SELECT, Action.WAIT});
        stageAction.add(new Action[]{Action.SELECT, Action.WAIT});
        stageAction.add(new Action[]{Action.WAIT, Action.SELECT});
        stageAction.add(new Action[]{Action.FINISH_TWO, Action.FINISH_TWO});
    }
}
